export default{
    //命名空间
    namespaced:true,
    state:{
        name:"产品Product",
        products:[]
    },
    getters:{},
    mutations:{
        //获取产品数据
        //获取action的payload数据，进行修改
        SET_PRODUCT(state,payload){
            state.products = payload
        },
        //让库存减少
        DECREASE_INVENTORY(state,payload){
            //需要知道让哪一条数据的库存减少
            console.log(payload)
            // 将数据从state.products中查找出来，然后将inventory进行--
            let target = state.products.find(item => item.id === payload.id) 
            if(target){
                target.inventory--
            }

        },
    // 让库存增加
    INCREASE_INVENTORY(state,payload){
        // 通过payload的id找到state.products中对应的数据，让库存+1
        let target = state.products.find(item => item.id === payload.id)
        if(target){
            target.inventory++
        }
    }

    },
    actions:{
      async FETCH_PRODUCT({commit}){
       let res = await fetch('http://chst.vip/data/index.json').then(body=>body.json())
       console.log(res)
        // 提交给mutation进行修改
        commit('SET_PRODUCT',res.data)
      }  
    },

} 